namespace Xmef.Engine
{
    using System;
    using Xmef.Interfaces;

    [Serializable]
    public sealed class Property<T> : Property, IProperty<T>, IConvertible<T>, IEquatable<T>, IProperty, IPluginIdentity
    {
        private IPropertyConverter<T> _converter;
        private T _defaultValue;
        private T _value;

        public Property(Property property) : base(property)
        {
            this._value = PropertyConverter.ConvertFromString<T>(property.Value);
        }

        public Property(Property property, T defaultValue) : base(property)
        {
            this._value = PropertyConverter.ConvertFromString<T>(property.Value);
            this._defaultValue = defaultValue;
        }

        public Property(Property property, T defaultValue, IPropertyConverter<T> converter) : base(property)
        {
            this._value = PropertyConverter.ConvertFromString<T>(converter, property.Value);
            this._defaultValue = defaultValue;
            this._converter = converter;
        }

        public bool Equals(T obj)
        {
            if (this._converter != null)
            {
                return (string.Compare(Value, PropertyConverter.ConvertToString<T>(this._converter, obj)) == 0);
            }
            return (string.Compare(Value, PropertyConverter.ConvertToString<T>(obj)) == 0);
        }

        public T GetValue()
        {
            if (this._converter != null)
            {
                return PropertyConverter.ConvertFromString<T>(this._converter, Value);
            }
            return PropertyConverter.ConvertFromString<T>(Value);
        }

        public T GetValue(IPropertyConverter<T> converter)
        {
            if (converter != null)
            {
                return PropertyConverter.ConvertFromString<T>(converter, Value);
            }
            return this.GetValue();
        }

        public void SetValue(T value)
        {
            if (this._converter != null)
            {
                Value = PropertyConverter.ConvertToString<T>(this._converter, value);
            }
        }

        public void SetValue(T value, IPropertyConverter<T> converter)
        {
            if (converter != null)
            {
                this.SetValue(value, converter);
            }
            this.SetValue(value);
        }

        public T DefaultValue
        {
            get
            {
                return this._defaultValue;
            }
        }
    }
}

